package cc.linker.steplink.crm.merchanth5.service;

import cc.linker.steplink.crm.merchanth5.dao.ColumnDao;
import cc.linker.steplink.crm.merchanth5.dao.ZsContentDao;
import cc.linker.steplink.crm.merchanth5.service.redis.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cc.linker.steplink.crm.merchanth5.service.ZsContentService.CONTENT_PLAY_TIMES;
import static cc.linker.steplink.crm.merchanth5.service.ZsContentService.CONTENT_SCHEDULE;

@Service
public class ColumnService {

    @Autowired
    private ColumnDao columnDao;
    @Autowired
    private MaiBaoLaService maiBaoLaService;
    @Autowired
    private ZsContentDao zsContentDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private GoodsService goodsService;

    public Map<String, Object> getDetail(String goodsId, String customerId) {

        Map<String, Object> detail = goodsService.getCloumnGoodsInfo(customerId, goodsId);

        maiBaoLaService.judgeDeleteOrBanned(detail);
        Map<String, Object> map = new HashMap<>();
        map.put("customerId", customerId);
        map.put("goodsId", goodsId);
        map.put("columnGoodsId", "");
        boolean ifPurchase = this.ifPurchase(map);
        detail.put("purchased", ifPurchase ? 1 : 0);
        String filePath = redisTemplate.opsForValue().get("idp_sourceImg_" + goodsId);
        if (StringUtil.isNotEmpty(filePath)) {
            detail.put("goodsPreview", filePath);
        } else {
            detail.put("goodsPreview", null);
        }
        return detail;
    }

    public List<Map<String, Object>> getColumnContents(Integer integralGoodsId,String columnId, String customerId, String orderBy) {

        List<Map<String, Object>> list = goodsService.getColumnContents(columnId, orderBy);
        PageHelper.clearPage();

        Map<String, Object> detail = columnDao.getDetailByColumnId(columnId);
        Integer columnIsFree = (Integer) detail.get("isFree");
        Long goodsId = (Long) detail.get("goodsId");

        Map<String, Object> param = new HashMap<>();
        param.put("customerId", customerId);
        param.put("columnGoodsId", goodsId);

        // 1.当内容为音频且已经购买（专栏购买，单篇购买都算购买）时，展示播放按钮
        // 2.当内容为音频且免费（被设置为“免费浏览”或者专栏为免费）时，展示播放按钮
        for (Map<String, Object> map : list) {
            long goodsIdL = Long.valueOf(map.get("goodsId").toString());
            map.put("integralGoodsId",null);
            int isIntegralGoods = columnDao.getIntegralGoodsIds(goodsIdL,integralGoodsId);
            if(isIntegralGoods > 0){
                map.put("integralGoodsId",integralGoodsId);
            }
            Long contentId = Long.valueOf(map.get("contentId").toString());
            Object o = redisTemplate.opsForHash().get(CONTENT_SCHEDULE + contentId, customerId);
            if (!StringUtils.isEmpty(o)) {
                map.put("schedule", o.toString());
            } else {
                map.put("schedule", null);
            }
            // 播放数
            Long size = redisTemplate.opsForSet().size(CONTENT_PLAY_TIMES + contentId);
            Integer viewNum = (Integer) map.get("viewNum");
            if (size == null) {
                size = 0L;
            }
            map.put("viewNum", viewNum + size);

            // goodsId columnGoodsId customerId
            param.put("goodsId", map.get("goodsId"));

//            Integer isPurchase = zsContentDao.getPurchaseCount(param);
            boolean isPurchase = this.ifPurchase(param);
            if (isPurchase) {
                // 买过了
                map.put("isPurchase", 1);
                continue;
            } else {
                map.put("isPurchase", 0);
            }
            if (columnIsFree == 1) {
                // 专栏免费
                continue;
            }
            String contentComplete = (String) map.get("contentComplete");
            if (StringUtils.isEmpty(contentComplete)) {
                // 没资源
                continue;
            }
            Integer isFree = (Integer) map.get("isFree");
            if (isFree == 1) {
                // 内容免费
                continue;
            }
            map.put("contentComplete", null);
        }
        return list;
    }

    public Map<String, Object> getOrder(String columnId) {
        return columnDao.getOrder(columnId);
    }

    public String getSortByColumnId(String columnId) {
        Integer sort = columnDao.getSortByColumnId(columnId);
        if (sort == null) {
            return "desc";
        }
        return sort == 0 ? "desc" : "asc";
    }

    /**
     * 是否购买过
     *
     * @param goodsId       商品id
     * @param columnGoodsId 专栏商品id
     * @param customerId    用户Id
     * @return boolean
     */
    public boolean ifPurchase(Map<String, Object> map) {
        map.putIfAbsent("columnGoodsId", "");
        Integer purchaseCount = zsContentDao.getPurchaseCount(map);
        if (purchaseCount > 0) {
            return true;
        }
        Integer exercisePurchaseCount = zsContentDao.getExercisePurchaseCount(map);
        if (exercisePurchaseCount > 0) {
            return true;
        }
        Integer integralCount = zsContentDao.getIntegralCount(map);
        return integralCount > 0;
    }
}
